package cl.tieba.core.exception.dispose;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;

import tieba.core.exception.BaseException;
import tieba.core.utils.ReturnType;

public class TbExceptionMapper implements ExceptionMapper<Exception> {

	@Override
	public Response toResponse(Exception e) {
		if (e instanceof BaseException) {
			return ReturnType.fail(((BaseException) e),
					((BaseException) e).getData());
		} else {
			if (e instanceof com.sun.jersey.api.NotFoundException) {
				return Response.ok("404 error. not found!").status(Status.NOT_FOUND).build();
			} else {
				return ReturnType.fail(500, e.getMessage());
			}
		}
	}

}
